package com.first.servers.iml;

import com.first.dao.UsersMapper;
import com.first.entity.Users;
import com.first.entity.UsersVO;
import com.first.servers.UsersServers;
import com.common.base.Result;
import com.kckj.comm.util.ExcelUtil;
import com.kckj.comm.validation.AbstractValidationCell;
import com.kckj.util.RedisUtil;
import com.kckj.comm.util.DateUtils;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@ComponentScan(basePackages = {"com.kckj"})
public class UsersServerIml implements UsersServers {
    @Resource
    private UsersMapper usersMapper;
    @Resource
    private RedisUtil redisUtil;




    @Override
    public List<Users> getAllUsers() {
        return usersMapper.getAllUsers();
    }


    @Override
    public String str() {
       return sys();
    }

    @Override
    public String saveRedisData() {
        Users users = new Users();
        users.setUserName("admin");
        users.setPassWord("1234567");
        users.setMobile("15232189166");
        Boolean bl = redisUtil.set("chaoqiang","chaoqang");
        boolean bl2 = redisUtil.set("USER:chaoqiang",users,30);
        if(bl){
            return "插入redis数据成功！,数据展示："+redisUtil.get("USER:chaoqiang");
        }else{
            return "插入redis数据失败！";
        }

    }

    @Override
    public String findUtilDate() {
        Date date = DateUtils.getWeekStart();
        Date date1 = DateUtils.getWeekEnd();
        String str = DateUtils.getDatePoor(date1,date);
        Date date2 = new Date();
        //加时间按照毫秒计算
       String str2= DateUtils.addMillisecond(date2,1000*60*60*24*2);
        return str2;
    }

    @Override
    public List<UsersVO> exportUsers() {
        List<UsersVO> usersVOList = usersMapper.exportUsers();
        return usersVOList;
    }

    @Override
    public String importUsers(MultipartFile file) {
        try {
            ExcelUtil<UsersVO> listUsers = new ExcelUtil<>(UsersVO.class);
            Map<Integer,AbstractValidationCell> map =new HashMap<>();
            List<UsersVO> usersVOList = listUsers.importExcel(file.getInputStream(),0,1,map);
            Integer num = usersMapper.importUsersList(usersVOList);
            if(num>0){
                return "导入成功";
            }else{
                return "导入失败";
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
            return "导入失败，请联系管理员";
        }
    }

    public String sys(){
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=simpleDateFormat.format(date);
        System.out.println("定时任务出现"+time);
        return time;
    }
}
